#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace MIMP
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        bool isGameInProgress;

        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen(bool isGameInProgress)
            : base("Main Menu")
        {
            this.isGameInProgress = isGameInProgress;
            // Create our menu entries.
            MenuEntry newGameMenuEntry = new MenuEntry("New Game");
            MenuEntry loadGameMenuEntry = new MenuEntry("Load Game");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");

            // Hook up menu event handlers.
            newGameMenuEntry.Selected += NewGameMenuEntrySelected;
            loadGameMenuEntry.Selected += LoadGameMenuEntrySelected;
            exitMenuEntry.Selected += ExitGameMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(newGameMenuEntry);
            MenuEntries.Add(loadGameMenuEntry);
            if (isGameInProgress)
            {
                MenuEntry saveGameMenuEntry = new MenuEntry("Save Game");
                saveGameMenuEntry.Selected += SaveGameMenuEntrySelected;
                MenuEntries.Add(saveGameMenuEntry);
            }

            MenuEntries.Add(exitMenuEntry);
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void NewGameMenuEntrySelected(object sender, EventArgs e)
        {
            //LoadingScreen.Load(ScreenManager, true, new GameplayScreen());
            ScreenManager.AddScreen(new LoadGameScreen(MIMPGame.MAPS_DIR, true));
        }


        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void LoadGameMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new LoadGameScreen(MIMPGame.SAVED_GAMES_DIR, false));
        }

        void SaveGameMenuEntrySelected(object sender, EventArgs e)
        {
            KeyboardInputScreen saveDialog = new KeyboardInputScreen("Enter a name for this saved game.");
            saveDialog.Accepted += SaveGameHandler;
            ScreenManager.AddScreen(saveDialog);
        }

        void SaveGameHandler(object sender, StringEventArgs e)
        {
            IGameStateService gs = (IGameStateService)ScreenManager.Game.Services.GetService(typeof(IGameStateService));
            gs.SaveGame(e.Message);
            ExitScreen();
        }

        void ExitGameMenuEntrySelected(object sender, EventArgs e)
        {
            const string message = "Are you sure you want to exit?";

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox);
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            if (!isGameInProgress)
            {
                ExitGameMenuEntrySelected(this, new EventArgs());
            }
            else
            {
                base.OnCancel();
            }
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
        {
            ScreenManager.Game.Exit();
        }


        #endregion

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
    }
}
